package com.wedo.mall.widget;

import android.content.Context;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.e.library.utils.EUtils;
import com.wedo.mall.R;
import com.wedo.mall.activity.comment.ImageActivity;
import com.wedo.mall.util.WUrl;

import java.util.ArrayList;
import java.util.List;

/**
 * 网格图片控件
 * Created by e on 2017/7/9.
 */

public class GridImage extends LinearLayout {

    private int mSpanCount = 3;
    private int mSize = 182;
    private int mMargin = 5;

    private ArrayList<String> mImages = new ArrayList<>();

    public GridImage(Context context) {
        super(context);
    }

    public GridImage(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public GridImage(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public void image(List<String> images) {
        setOrientation(VERTICAL);
        removeAllViews();
        mImages.clear();

        if (EUtils.isEmpty(images)) {
            setVisibility(GONE);
            return;
        }
        mImages.addAll(images);
        setVisibility(VISIBLE);
        LinearLayout line = null;
        int count = images.size();
        for (int i = 0; i < count; i++) {
            String image = images.get(i);
            View item = item(image,i);

            int value = i % mSpanCount;
            if (value == 0){
                line = line();//新增一行
            }

            line.addView(item,params());

            if (value == 0){
                this.addView(line);//新增一行添加到View上
            }
        }
    }

    private LayoutParams params(){
        LayoutParams params = new LayoutParams(mSize, mSize);
        params.leftMargin = mMargin;
        params.rightMargin = mMargin;
        params.bottomMargin = mMargin;
        params.topMargin = mMargin;
        return params;
    }

    private View item(String image, final int position) {
        String img = WUrl.url(image);
        ImageView iv = new ImageView(getContext());
        iv.setScaleType(ImageView.ScaleType.FIT_XY);
        Glide.with(getContext()).load(img).thumbnail(0.4f)
                .error(R.drawable.place_holder_mrt_02)
                .placeholder(R.drawable.place_holder_mrt_02)
                .diskCacheStrategy(DiskCacheStrategy.ALL).into(iv);

        iv.setClickable(true);
        iv.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                ImageActivity.images(getContext(),mImages,position);
            }
        });
        return iv;

    }

    private LinearLayout line() {
        LinearLayout l = new LinearLayout(getContext());
        l.setOrientation(HORIZONTAL);
        LinearLayout.LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
        l.setLayoutParams(params);
        return l;
    }

}
